package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Option;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wjs
 * @version 1.0
 * @date 2022/1/9 0009 上午 9:05
 */
@DubboService
public class OptionApiImpl implements OptionApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Option> findByIds(List<String> optionIds) {
        List<ObjectId> ids = optionIds.stream().map(id -> new ObjectId(id)).collect(Collectors.toList());
        Query query = Query.query(Criteria.where("id").in(ids));
        return mongoTemplate.find(query,Option.class);
    }

    @Override
    public void save(Option option) {
        mongoTemplate.save(option);
    }
}
